<?php
/**
 * @package   auto_occasion
 * @subpackage 
 * @author    CMS
 * @copyright  2011
 * @license    All rights reserved
 */

class chapitreSrv {

     /**
     * Fonction permettant de sauvegarder les chapitres d'un essai
     * @return object $partenaire
     *
     */
    public function ajouter ($chapitre) {
        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_essais_chapitre");
        $records   = jDao::createRecord("commun~ao_essais_chapitre");

        $records->chap_name_id  = $chapitre['chap_name_id'];
        $records->chap_content  = trim($chapitre['chap_content']);
        //$records->chap_position = $chapitre['chap_position'];
        $records->chap_img      = $chapitre['chap_img'];
        $records->type          = $chapitre['type'];
        $records->chap_modele   = $chapitre['chap_modele'];
        $records->chap_titre    = trim($chapitre['chap_titre']);
		
        if ($maFactory->insert($records)) {
            return $records->chap_id;
        }

    }
     /**
     * Fonction permettant de modifier les chapitres d'un essai
     * @return object $partenaire
     *
     */
    public function modifier ($chapitre) {

        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_essais_chapitre");
        $records   = $maFactory->get($chapitre['chap_id']);

        $records->chap_name_id  = $chapitre['chap_name_id'];
        $records->chap_content  = trim($chapitre['chap_content']);
        $records->chap_position = $chapitre['chap_position'];
        $records->chap_img      = $chapitre['chap_img'];
        $records->type          = $chapitre['type'];
        $records->chap_modele   = $chapitre['chap_modele'];
        $records->chap_titre    = trim($chapitre['chap_titre']);

        if ($maFactory->update($records)) {
            return $chapitre['chap_id'];
        }

    }

    /**
     * Fonction permettant de sauvegarder le nom d'un chapitre
     * @param   $chapitre
     * @return  int id du chapitre créé
     */
    public static function saveChapitreName ($chapitre) {

        $oFac = jDao::get('commun~ao_chapitre_names') ;
        $oRecord = jDao::createRecord('commun~ao_chapitre_names') ;

       
        $oRecord->chap_name = $chapitre;
        $oFac->insert($oRecord) ;

        return $oRecord->chap_name_id;
        
    }

     /**
     * Fonction permettant de sauvegarder le nom d'une version
     * @param   $version
     * @return  int id du chapitre créé
     */
    public static function saveVersionName ($version) {

        $oFac = jDao::get('commun~ao_version_names') ;
        $oRecord = jDao::createRecord('commun~ao_version_names') ;

       
        $oRecord->version_name = $version;
        $oFac->insert($oRecord) ;

        return $oRecord->version_name_id;
        
    }


    /**
     * FO essai : liste des chapitres dans le sommaire
     * @return 
     */
     public function loadChapter_($essaiId, $modeleId=0, $type) {
        $sql  = " SELECT   chap_id,chap_content, chap_ess_id, ess_id, chap_modele, chap_img,  chap_name, chap_titre,fiabilite_id, rubrique_id, type, M.modele_libelle AS modele1, M2.modele_libelle AS modele2, E.ess_zVersion, E.ess_iTypeId
                FROM ao_essais_chapitre P 
                LEFT JOIN ao_chapitre_names N ON N.chap_name_id = P.chap_name_id 
                LEFT JOIN ao_essais E ON E.ess_id = P.chap_ess_id 
                LEFT JOIN ao_modele M ON M.modele_id = P.chap_modele
                LEFT JOIN ao_modele M2 ON M2.modele_id = E.ess_iModelId

                WHERE chap_ess_id IN ($essaiId)";

        // si le paramere modele est passé 
        if ($modeleId) 
            $sql .= " AND P.chap_modele = $modeleId";
        if ($type) 
            $sql .= " AND P.type = $type";
		
        $cnx = jDb::getConnection();
        $rs  = $cnx->query($sql);
        $res = array();
        while ($row = $rs->fetch()) {
            if ($row->chap_content) {
                $content = $row->chap_content;
                $filename = JELIX_APP_WWW_PATH . PATH_ESSAI_SOUSRUBRIQUE . 'guide' . $essaiId . '/' . $content;
                if (strpos($content, ".txt") !== false) {
                    //récupération du contenu du fichier et le mettre dans la variable ess_zChapeau x :gamme-porsche-1019663438.txt
                    jClasses::inc('commun~toolsFile');
                    $row->chap_content = toolsFile::get($filename);
                }
            }
            // récupération des images
            $photo = $const = array();
            if (strpos($row->chap_img, ',') !== false) {
                $tphotoChap = explode(',', $row->chap_img);
                foreach ($tphotoChap as $item) {
                    $withoutConst = 0;
                    if ($item && is_file(JELIX_APP_WWW_PATH . $item)) {
                        $withoutConst = 1;
                    }
                    $photo[] = $item;
                    $const[] = $withoutConst;
                }
            } else if ($row->chap_img && is_file(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $row->chap_img)) {
                $photo[] = $row->chap_img;
                $const[] = 0;
            } else if ($row->chap_img && is_file(JELIX_APP_WWW_PATH . $row->chap_img)) {
                $photo[] = $row->chap_img;
                $const[] = 1;
            }
            $row->image        = $photo;
            $row->withoutConst = $const;

            $res[] = $row;
        }

        return $res;
     }

         /**
     * FO essai : liste des chapitres dans le sommaire
     * @return 
     */
     public function loadModele($essaiId, $modeleId=0, $type) {
        $sql  = " SELECT   chap_id, chap_content, chap_ess_id, ess_id, chap_modele, chap_img,  chap_name, chap_titre,fiabilite_id,type, M.modele_libelle AS modele1, M2.modele_libelle AS modele2
                FROM ao_essais_chapitre P 
                LEFT JOIN ao_chapitre_names N ON N.chap_name_id = P.chap_name_id 
                LEFT JOIN ao_essais E ON E.ess_id = P.chap_ess_id 
                LEFT JOIN ao_modele M ON M.modele_id = P.chap_modele
                LEFT JOIN ao_modele M2 ON M2.modele_id = E.ess_iModelId

                WHERE chap_ess_id IN ($essaiId)";
        $sql .= " AND P.chap_modele <>'0'";

        // si le paramere modele est passé 
        if ($modeleId) 
            $sql .= " AND P.chap_modele = $modeleId";
        if ($type) 
            $sql .= " AND P.type = $type";

        $cnx = jDb::getConnection();
        $rs  = $cnx->query($sql);
        $res = array();
        while ($row = $rs->fetch()) {
            if ($row->chap_content) {
                $content = $row->chap_content;
                $filename = JELIX_APP_WWW_PATH . PATH_ESSAI_SOUSRUBRIQUE . 'guide' . $essaiId . '/' . $content;
                if (strpos($content, ".txt") !== false) {
                    //récupération du contenu du fichier et le mettre dans la variable ess_zChapeau x :gamme-porsche-1019663438.txt
                    jClasses::inc('commun~toolsFile');
                    $row->chap_content = toolsFile::get($filename);
                }
            }
            // récupération des images
            $photo = $const = array();
            if (strpos($row->chap_img, ',') !== false) {
                $tphotoChap = explode(',', $row->chap_img);
                foreach ($tphotoChap as $item) {
                    $withoutConst = 0;
                    if ($item && is_file(JELIX_APP_WWW_PATH . $item)) {
                        $withoutConst = 1;
                    }
                    if ($item && is_file(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $item)) {
                        $withoutConst = 0;
                    }
                    $photo[] = $item;
                    $const[] = $withoutConst;
                }
            } else if ($row->chap_img && is_file(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $row->chap_img)) {
                $photo[] = $row->chap_img;
                $const[] = 0;
            } else if ($row->chap_img && is_file(JELIX_APP_WWW_PATH . $row->chap_img)) {
                $photo[] = $row->chap_img;
                $const[] = 1;
            }
            $row->image        = $photo;
            $row->withoutConst = $const;

            $res[] = $row;
        }

        return $res;
     }


     /**
     * Bloc left : grouper les images , versions, fiabilité par modèle de gamme 
     * @return 
     */
     public static function getVersionByModele($iId,$modele) {
         $res= array();
         foreach($modele as $k=>$item) {
            if((isset($chapitre[$k+1]->chap_modele) && $item->chap_modele != $chapitre[$k+1]->chap_modele) || !isset($chapitre[$k+1]->chap_modele)){

                     $sql  = "SELECT version_name,version_name_id,chap_content FROM ao_version_names " ;
                     $sql.= " INNER JOIN ao_essais_chapitre ON ao_essais_chapitre.chap_name_id=  ao_version_names.version_name_id ";
                     $sql.= " WHERE ao_essais_chapitre.chap_modele =".$item->chap_modele  ;
                     $sql.= " AND ao_essais_chapitre.chap_ess_id =".$iId ; 
                     $sql.= " AND type = 2" ;

                    $cnx  = jDb::getConnection();
                    $rs   = $cnx->query($sql);

                    $version = array();
                    $result  = new stdClass();
                    while($row = $rs->fetch()){
                        $version[]        = $row;
                    }

                   $result->version     = $version;
                   $result->chap_id     = $item->chap_id;
                   $result->chap_ess_id = $item->chap_ess_id;
                   $result->ess_id      = $item->ess_id;
                   $result->chap_modele = $item->chap_modele;
                   $result->chap_img    = $item->chap_img;
                   $result->chap_name   = $item->chap_name;
                   $result->chap_titre  = $item->chap_titre;
                   $result->modele1     = $item->modele1;
                   $result->modele2     = $item->modele2;
                   $result->image       = $item->image;
                   $result->withoutConst = $item->withoutConst;
                   $res[] = $result;
           }
        }
        return $res;
     }

      /**
     *Afficher les versions d'un modèle d'une gamme 
     * @return 
     */
     public static function getVersionByChapModele($iId,$iChapModele) {
		$oConnexion = jDb::getConnection();
		$zQuery = "
					SELECT 
						version_name,
						version_name_id,
						chap_content ,
						chap_img
					FROM ao_version_names  
						LEFT JOIN ao_essais_chapitre 
						ON(ao_essais_chapitre.chap_name_id	=  ao_version_names.version_name_id)
						WHERE ao_essais_chapitre.chap_modele=  $iChapModele
						AND ao_essais_chapitre.chap_ess_id	=  $iId AND TYPE = 2
				  " ;

        $rs  = $oConnexion->query($zQuery);
        $res = array();
        while ($row = $rs->fetch()) {
            if ($row->chap_content) {
                $content = $row->chap_content;
                $filename = JELIX_APP_WWW_PATH . PATH_ESSAI_VERSION . 'guide' . $iId . '/' . $content;
                if (strpos($content, ".txt") !== false) {
                    //récupération du contenu du fichier et le mettre dans la variable 
                    $row->chap_content = toolsFile::get($filename);
                }
                $res[] = $row;
            }
        }
        return $res;
     }


         /**
     * FO essai : liste des chapitres dans le sommaire
     * @return 
     */
     public function loadChapter_gamme($essaiId,$id=0, $type) {

         $sql  = " SELECT" ;
         $sql.= " CHP.chap_name, CHP.chap_name_id, CHPE.chap_modele, CHPE.chap_id, ";
         $sql.= " CHPE.chap_img, CHPE.chap_position, CHPE.chap_name_id, chap_content, chap_position, CHPE.chap_id,CHPE.chap_ess_id as ess_id, type, CHPE.chap_id, ";
         $sql.= " (SELECT modele_libelle FROM ao_modele WHERE modele_id = CHPE.chap_modele) AS modele_libelle ";
         $sql .= " FROM  ao_essais_chapitre CHPE ,";
         $sql .= " ao_chapitre_names CHP";
         $sql .= " WHERE CHPE.chap_name_id = CHP.chap_name_id";
         $sql .= " AND CHPE.type = $type";
         $sql .= " AND CHPE.chap_modele = 0";

        if ($essaiId)
            $sql .= " AND CHPE.chap_ess_id = " . $essaiId;
        if ($id > 0)
            $sql .= " AND CHPE.chap_name_id = " . $id;
            $sql .= " GROUP BY CHP.chap_name_id ";
        $sql .= " ORDER BY CHPE.chap_position, CHPE.chap_id";
        $cnx = jDb::getConnection();
        $rs     = $cnx->query($sql);
        $res = array();
        while ($row = $rs->fetch()) {
            if ($row->chap_content) {
                $content = $row->chap_content;
                $filename = JELIX_APP_WWW_PATH . PATH_ESSAI_SOUSRUBRIQUE . 'guide' . $row->ess_id . '/' . $content;
                if (strpos($content, ".txt") !== false) {
                    //récupération du contenu du fichier et le mettre dans la variable ess_zChapeau x :gamme-porsche-1019663438.txt
                    jClasses::inc('commun~toolsFile');
                    $row->chap_content = toolsFile::get($filename);
                }
            }
            $ext = explode(",",  $row->chap_img);
            $t = array();
            foreach ($ext as $img) {
                if ($img && (is_file(JELIX_APP_WWW_PATH . $img))) {
                    $t[] = $img;
                }elseif($img && is_file(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $img)){
                    $t[] = PATH_ESSAI_IMG . $img;
                }
            }
            $row->chap_img = $t;
            $res[] = $row;
        }
        return $res;
     }


    /**
     * Bloc left : récupération des chapitres
     * @return 
     */
     public function loadChapter($essaiId = 0, $id = 0,$type=0) {
        $sql  = " SELECT" ;
        if($type==1)
            $sql.= " CHP.chap_name, CHP.chap_name_id, CHPE.chap_modele, CHPE.chap_id, ";
        else
            $sql.= " V.version_name, ";

        $sql.= " CHPE.chap_img, CHPE.chap_position, CHPE.chap_name_id, chap_content, chap_position, CHPE.chap_id,CHPE.chap_ess_id as ess_id, type, CHPE.chap_id,CHPE.chap_modele, ";
        $sql.= " (SELECT modele_libelle FROM ao_modele WHERE modele_id = CHPE.chap_modele) AS modele_libelle ";
        $sql .= " FROM  ao_essais_chapitre CHPE ,";
        if($type==1){
            $sql .= " ao_chapitre_names CHP";
            $sql .= " WHERE CHPE.chap_name_id = CHP.chap_name_id";
        } else{
            $sql .= " ao_version_names V";
            $sql .= " WHERE CHPE.chap_name_id = V.version_name_id";
        }
        if (!$type || ($type < 2)) 
            $sql .= " AND CHPE.type = 1";
        else 
            $sql .= " AND CHPE.type = 2";

        if ($essaiId)
            $sql .= " AND CHPE.chap_ess_id = " . $essaiId;
        if ($id > 0)
            $sql .= " AND CHPE.chap_name_id = " . $id;
        if($type==1)
            $sql .= " GROUP BY CHP.chap_name_id ";
        $sql .= " ORDER BY CHPE.chap_position, CHPE.chap_id";
		
        $cnx = jDb::getConnection();
        $rs     = $cnx->query($sql);
        $res = array();

        while ($row = $rs->fetch()) {
			
			//Preparation du contenu du chapitre
            if ($row->chap_content) {
                $content = $row->chap_content;
                if($type == 1)
                    $filename = JELIX_APP_WWW_PATH . PATH_ESSAI_SOUSRUBRIQUE . 'guide' . $row->ess_id . '/' . $content;
                if ($type == 2 )
                    $filename = JELIX_APP_WWW_PATH . PATH_ESSAI_VERSION . 'guide' . $row->ess_id . '/' . $content;
                if (strpos($content, ".txt") !== false) {
                    //récupération du contenu du fichier et le mettre dans la variable ess_zChapeau x :gamme-porsche-1019663438.txt
                    jClasses::inc('commun~toolsFile');
                    $row->chap_content = toolsFile::get($filename);
                }
            }
			
            $ext = explode(",",  $row->chap_img);
            $t = array();

            if (sizeof($ext)) {
                foreach ($ext as $img) {
					$img				=  str_replace('/auto-occasion/srcs/' ,'',$img) ;
					$row->chap_img		=  str_replace('/auto-occasion/srcs/' ,'',$row->chap_img) ;
                    $directPath			=  realpath(JELIX_APP_WWW_PATH . '../../'); // utile sur le serveur de test
                    if (file_exists(JELIX_APP_WWW_PATH . $img)) {
                        $t[] = $img;
                    } elseif (file_exists(JELIX_APP_WWW_PATH.PATH_ESSAI_IMG . $img)){
                        $t[] = PATH_ESSAI_IMG . $img;

                    } elseif (file_exists($directPath . PATH_ESSAI_IMG . $img)){
                        $t[] = PATH_ESSAI_IMG . $img;
                    } elseif (file_exists($directPath . $img)){
                        $t[] = $img;
                    }
                }
            } else if ($row->chap_img && is_file(JELIX_APP_WWW_PATH.PATH_ESSAI_IMG . $row->chap_img)) {
               $t[] = PATH_ESSAI_IMG . $row->chap_img;
            } else if ($row->chap_img && is_file(JELIX_APP_WWW_PATH.$row->chap_img)) {
               $t[] = $row->chap_img;
            } else if ($row->chap_img && is_file($directPath . $row->chap_img)) {
               $t[] = $row->chap_img;
            } else if ($row->chap_img && is_file($directPath . PATH_ESSAI_IMG . $row->chap_img)) {
               $t[] = PATH_ESSAI_IMG . $row->chap_img;
            }
			
            $row->chap_img = $t;
                
           $res[] = $row;
        }

		
        return $res;
     }


    /**
     * Bloc left : grouper les images des chapitres
     * @return 
     */
     public static function groupImgByChapter($chapitre) {

         $res = array();
         if ($chapitre) {
             foreach ($chapitre as $k=>$item) {
                 //$photo_img = implode(',',$item->chap_img);
                 if((isset($chapitre[$k+1]->chap_name_id) && $item->chap_name_id != $chapitre[$k+1]->chap_name_id) || !isset($chapitre[$k+1]->chap_name_id)){
                     $chapDetail					= new stdClass();
                     if($item->type==1)
                        $chapDetail->name			= $item->chap_name;
                     else
                        $chapDetail->name			= $item->version_name;
                     $chapDetail->chap_id			= $item->chap_id;
                     $chapDetail->type				= $item->type;
                     $chapDetail->ess_id			= $item->ess_id;
                     $chapDetail->chap_name_id		= $item->chap_name_id;
                     $chapDetail->img				= $item->chap_img;
                     $res[] = $chapDetail;
                     unset($image);
                 }
             }
         }

         return $res;
     }

    /**
     * insert into ao_essais_link_chapitre
     * @param int $essai
     * @param int $chap
     * @return
     */
    public static function insertEssaiChapitre($ess_id, $chap){
		
        $query = " UPDATE ao_essais_chapitre SET chap_ess_id = $ess_id, rubrique_id = $chap, fiabilite_id = 0 WHERE chap_id = $chap" ;
        
        $cnx = jDb::getConnection();

        return $cnx->exec($query);
    }

      /**
     * Update table ao_essais_chapitre after updating  the field 'fiabilite_id' of table ao_essais_rappel   
     * @param int $ess_is
     * @param int $ess_is
     * @return
     */
    public static function updateEssaiChapitre($rappelId, $ess_id, $chapModele){

        $query  = " UPDATE ao_essais_chapitre SET fiabilite_id = $rappelId "; 
		$query .= " WHERE  fiabilite_id = 0 AND type = 1   " ;
		if($ess_id) {
			 $query .= "  AND chap_ess_id = $ess_id  " ;
		}
       
        if ($chapModele) 
            $query .= " AND chap_modele = $chapModele" ;
        $cnx = jDb::getConnection();

        return $cnx->exec($query);
    }

    /**
     * Renvoie tous les fiches d'une Categorie
     * @param array $_toParams
     * @return array 
     */
    public static function reOrder ($_toParams) {
        
        $oFac = jDao::get('commun~ao_essais_chapitre') ;
        
        $tiChapitreIds = array_key_exists('tiChapitreIds', $_toParams) ? $_toParams['tiChapitreIds'] : array() ;

        foreach ($tiChapitreIds as $iKey => $iChapitreId) {
            $oRec = $oFac->get($iChapitreId) ;
            $oRec->chap_position = $iKey ;
            $oFac->update($oRec) ;
        }
	}

        /**
     * Renvoie tous les fiches d'une Categorie
     * @param array $_toParams
     * @return array 
     */
    public static function reOrderVersion ($_toParams) {
        
        $oFac = jDao::get('commun~ao_essais_chapitre') ;
        
        $tiChapitreIds = array_key_exists('tiVersionIds', $_toParams) ? $_toParams['tiVersionIds'] : array() ;

        foreach ($tiVersionIds as $iKey => $iChapitreId) {
            $oRec = $oFac->get($iChapitreId) ;
            $oRec->chap_position = $iKey ;
            $oFac->update($oRec) ;
        }
	}

    /**
     * retourne une liste d'id des chapitres pour déterminer le next et prev
     */
    public static function getChapterIds($idEssai) {

        $sql  = " SELECT chap_id, chap_modele ";
        $sql .= " FROM ao_essais_chapitre";
        $sql .= " WHERE chap_ess_id= $idEssai AND type=1";
       
        $cnx  = jDb::getConnection();
        $rs   = $cnx->query($sql);

        $chapId = $modeleId = array();
        $result  = new stdClass();
        while($row = $rs->fetch()){
			$chapId[]   = $row->chap_id;
			$modeleId[] = $row->chap_modele;
        }
        $result->chapId   = $chapId;
        $result->modeleId = $modeleId;

		return $result;
    }
    /**
     * Renvoie le nom de chapitre précédent
     */
    public static function getChapterPrec ($idEssai,$position,$chapid) {

        $sql  = " SELECT CHP.chap_name,CHPE.chap_name_id,CHPE.chap_ess_id ";
        $sql .= " FROM ao_chapitre_names CHP, ao_essais_chapitre CHPE";
        $sql .= " WHERE CHPE.chap_name_id = CHP.chap_name_id";
        $sql .= " AND CHPE.type = 1";
        if ($idEssai)
            $sql .= " AND CHPE.chap_ess_id = " . $idEssai;
        if ($position<>0)
            $sql .= " AND CHPE.chap_position = " .($position-1)."  ORDER BY CHPE.chap_id DESC LIMIT 1";
        if ($position==0)
            $sql .= "  AND CHPE.chap_position = ".$position. " AND CHPE.chap_id <".$chapid ." ORDER BY CHPE.chap_id DESC LIMIT 1";
         $jdb = jDb::getDbWidget();
        
        $return = $jdb->fetchAll($sql);
		return $return;

    }

     /**
     * Renvoie le nom de chapitre précédent
     */
    public static function getChapterSuiv ($idEssai,$position,$chapid=0) {

        $sql  = " SELECT CHP.chap_name,CHPE.chap_name_id,CHPE.chap_ess_id  ";
        $sql .= " FROM ao_chapitre_names CHP, ao_essais_chapitre CHPE";
        $sql .= " WHERE CHPE.chap_name_id = CHP.chap_name_id";
        $sql .= " AND CHPE.type = 1";
        if ($idEssai)
            $sql .= " AND CHPE.chap_ess_id = " . $idEssai;
        if ($position<>0)
            $sql .= " AND CHPE.chap_position = ".($position+1);
        if ($position==0){
            $id   = self::getChapterSuivPos ($idEssai,$chapid );
            if($id){
                $sql .= "  AND CHPE.chap_position = ".$position. " AND CHPE.chap_id >".$chapid ." LIMIT 1";
            } else{
                $sql .= "  AND CHPE.chap_position = ".($position+1);
            }
        }
        $jdb = jDb::getDbWidget();

        $return = $jdb->fetchAll($sql);
		return $return;

    }

    /**
     * Renvoie le nom de chapitre suivant de position 0
     */
    public static function getChapterSuivPos ($idEssai,$chapid ) {

        $sql  = " SELECT CHPE.chap_id";
        $sql .= " FROM ao_chapitre_names CHP, ao_essais_chapitre CHPE";
        $sql .= " WHERE CHPE.chap_name_id = CHP.chap_name_id";
        $sql .= " AND CHPE.type = 1";
        if ($idEssai)
            $sql .= " AND CHPE.chap_ess_id = " . $idEssai;
        $sql .= "  AND CHPE.chap_position = 0 AND CHPE.chap_id >".$chapid ." LIMIT 1";
        $cnx = jDb::getConnection();
        $rs = $cnx->query($sql);
        $row = $rs->fetchAll();
        return (($row[0]->chap_id));
    }


     /**
     * Renvoie le nom des chapitres 
     */
    public static function getChapterTitre () {

        $sql  = " SELECT chn.chap_name_id,chn.chap_name FROM ao_chapitre_names chn ";
        $sql .= " INNER JOIN ao_chapitre ch ON ch.chap_name = chn.chap_name GROUP BY chn.chap_name ORDER BY ch.chap_id";
        $jdb = jDb::getDbWidget();
        $return = $jdb->fetchAll($sql);
		return $return;
        
    }

   /**
     * Renvoie le nom de chapitre précédent
     */
    public static function getChapterOther () {

        $sql  = " SELECT chn.chap_name_id FROM ao_chapitre_names chn ";
        $sql .= " INNER JOIN ao_chapitre ch ON ch.chap_name= chn.chap_name GROUP BY chn.chap_name";
        $jdb = jDb::getDbWidget();

        $res =array();
        $row = $jdb->fetchAll($sql);
		foreach ($row as $item){
                $res[] = $item->chap_name_id;
            }
        
        return $res;

    }

    /**
     * Renvoie le plus grand chap_id pour un essai
     */
    public static function getChapterMax($idEssai){

        $sql  = " SELECT MAX(chap_id) as chap_id FROM ao_essais_chapitre WHERE chap_ess_id =$idEssai AND type = 1 ";
        $jdb = jDb::getDbWidget();

        $res =array();
        $row = $jdb->fetchAll($sql);
		foreach ($row as $item){
                $res[] = $item->chap_id;
            }
        
        return $res;

    }


 
}